import sysApi from '@/api/sysApi';
import dictApi from '@/api/dictApi';
import { getToken, setToken, clearToken,setUserId, setCookies } from '@/utils/cookie';
import userApi from "@/api/userApi";

const sys = {
  state: {
    token: getToken() || '',
    dictionary: [],
    memuLists: []
  },
  mutations: {
    /**
     * 覆盖所有字典
     * @param {Object} state 状态
     * @param {Array} dictionary 字典列表
     * @returns {*} null
     */
    setDictionary (state, dictionary) {
      state.dictionary = dictionary;
    },
    setToken: (state, token) => { state.token = token; },
    setMemuLists: (state, memuLists) => { state.memuLists = memuLists; }
  },
  actions: {
    /**
     * 获取所有字典项
     * @param {Object} commit 提交信息
     * @param {Object} state 状态
     * @param {Object} params 状态
     * @returns {*|Promise} 响应
     * @constructor
     */
    GetDictionary ({ commit, state }, params) {
      if (!state.dictionary || state.dictionary.length === 0) {
        return new Promise(resolve => {
          dictApi.get(params).then(res => {
            commit('setDictionary', res);
            resolve(true);
          }).catch(() => resolve(false));
        });
      }
    },
    /**
     * 获取菜单权限
     */
    getEmeu ({ commit, state }, params) {
      return new Promise(resolve => {
        userApi.getMenu(params).then(res => {
          commit('setMemuLists', res);
          resolve(res);
        }).catch(() => resolve([]));
      });
    },
    /**
     * 获取指定系统中的字典项
     * @param {Object} commit 提交信息
     * @param {Object} state 状态
     * @param {String} sys 子系统
     * @param {String} dictTypeCode 类型id
     * @returns {*|Promise} 响应
     * @constructor
     */
    GetDictItems ({ commit, dispatch, state }, dictTypeCode) {
      return new Promise(resolve => {
        if (!state.dictionary || state.dictionary.length === 0) {
          dictApi.get().then(res => {
            commit('setDictionary', res);
            resolve(res.filter(dict => dictTypeCode === dict.DictTypeCode));
          }).catch(() => resolve([]));
        } else {
          if (!state.dictionary || state.dictionary.length < 1) resolve([]);
          resolve(state.dictionary.filter(dict => dictTypeCode === dict.DictTypeCode));
        }
      })
    },
    Login ({ commit, dispatch, state }, params) {
      return new Promise(resolve => {
        sysApi.login(params).then(({ Users, UsersProfile, Token }) => {
          if (!Users || !UsersProfile) resolve(false);
          commit('setId', UsersProfile.Id);
          commit('setProfile', UsersProfile);
          commit('setUsername', Users.Username);
          commit('setRealname', Users.Username);
          commit('setAvatar', Users.Avatar);
          commit('setStatus', Users.Status);
          commit('setToken', Token);
          setToken(Token);
          setUserId(UsersProfile.Id)
          setCookies('Username', Users.Username)
          // setCookies(UsersProfile.Id)
          dispatch('getEmeu');
          dispatch('GetDictionary');
          dispatch('GetBtnPermissions');
          resolve(true);
        }).catch(() => {
          resolve(false);
        });
      })
    },
    Logout ({ commit, state }) {
      return new Promise(resolve => {
        sysApi.logout().then(res => {
          if (res.code !== 1) resolve(false);
          clearToken();
          resolve(true);
        }).catch(() => resolve(false));
      })
    }
  }
};

export default sys;
